package com.practice.util;

import javax.sql.DataSource;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;

/**
 * Created by Administrator on 2016/8/21.
 */
public class IJdbcTemplate extends JdbcTemplate {
  public IJdbcTemplate() {
  }

  public IJdbcTemplate(DataSource dataSource) {
    super(dataSource);
  }

  public IJdbcTemplate(DataSource dataSource, boolean lazyInit) {
    super(dataSource, lazyInit);
  }

  @Override
  public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws
      DataAccessException {
    List<T> results = query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper, 1));
    if (results.isEmpty()) {
      return null;
    }
    return DataAccessUtils.requiredSingleResult(results);
  }
}
